import sys
import os
sys.path.append(os.path.abspath(os.path.dirname(__file__)) + "/..")
from stock.simulatestock import *
from utils.smtp.smtp import *
from utils.utils import *
import time

def simulate(code, array, follow = False, buy_now = False):
    kdjo = SimulateStock(code, 240, 100)
    stat = kdjo.KdjProc()
    if stat == None:
        return
    okn = stat["okn"]
    errorn = stat["errorn"]
    money_begin = stat["money_begin"]
    money_total = stat["money_total"]
    bi = stat["bi"]
    si = stat["si"]
    if okn <= 0:
        return
    rate = int(100.0*okn/(okn + errorn))
    stat["rate"] = rate
    stat["code"] = code
    if follow == True:
        array.append(stat)
        return
    if (okn < 4):
        return
    # #只给一次错误机会
    # if errorn > 1:
    #     return
    if rate < 75:
        return
    if buy_now == True:
        # 选出现在可能可买的
        if int(stat["LJ"]) < lmin and int(stat["J"]) > lmin:
            print("**** code %s rate %d okn=%d errorn=%d from %d to %d and costt %d and array len=%d"%
            (code, rate, okn, errorn, money_begin, money_total, si - bi, len(array)))
            array.append(stat)
    elif  int(stat["J"]) <= lmin:
        # 选出将来可能可买的
        print("**** code %s rate %d okn=%d errorn=%d from %d to %d and costt %d and array len=%d"%
        (code, rate, okn, errorn, money_begin, money_total, si - bi, len(array)))
        array.append(stat)

def takerate(item):
    return item["rate"]

def getstatstr(stat):
    str = ""
    okn = stat["okn"]
    errorn = stat["errorn"]
    money_begin = stat["money_begin"]
    money_total = stat["money_total"]
    bi = stat["bi"]
    si = stat["si"]
    rate = stat["rate"]
    code = stat["code"]
    str += ("\r\n**************************************************************\n")
    str += ("**** code %s rate %d okn=%d errorn=%d from %d to %d and costt %d lj %d cur j %d\n"%
        (code, rate, okn, errorn, money_begin, money_total, si - bi, int(stat["LJ"]),int(stat["J"])))
    for v in stat["itemnode"]:
        bt = v["bt"]
        st = v["st"]
        ct = v["ct"]
        get = v["get"]
        costm = v["costm"]
        str += ("code %s bt=%s st=%s ct=%d get=%d costm=%d\n"%
        (code, bt, st, ct, get, costm))
    return str

def savefile(array, file):
    f = open(file, "w")
    for stat in array:
        str = getstatstr(stat)
        f.write(str)
    f.close()
    print("%s write over"%(file))

def statshow(array):
    for stat in array:
        str = getstatstr(stat)
        print(str)

def kdj_select(buy_now = False):
    select_array = []
    codes = get_codes()
    for code in codes:
        simulate(code, select_array, False, buy_now)
    #排序个序
    select_array.sort(key = takerate, reverse=True)
    savefile(select_array, "select_code.txt")
    return select_array
    

def kdj_follow():
    select_array = []
    code = "sz300999"
    simulate(code, select_array, True)
    statshow(select_array)

def time_check():
    hours = get_hours()
    mins = get_mins()
    week = get_week()
    am = hours*60 + mins
    # print("hours=", hours, "mins=", mins, "am=", am, "week=", week)
    if week < 1 or week > 5:
        return False
    #定时8点发
    if am != 20*60:
        return False
    return True

def timer_select_proc():
    select_array = kdj_select(True)
    if len(select_array) == 0:
        print("there is no xxxxxxx")
        return
    str = "%s"%(get_local_time("%Y-%m-%d %H:%M:%S "))
    for stat in select_array:
        str += "%s "%(stat["code"])
    smtp = Smtp()
    print(str)
    smtp.Send(str)

#每天定时检查发邮件
def timer_select():
    while True:
        if time_check() == True:
            timer_select_proc()
        time.sleep(10)

if __name__ == "__main__":
    # kdj_select(True)
    # kdj_follow()
    timer_select()
    # timer_select_proc()
